import apiService from "@service/index";
import { saveUser } from "@slice/user";
import store from "@store/index";
// export const userApiService = apiService
// .enhanceEndpoints({ addTagTypes: ["profile"] })   // 
// // 获取用户资料
// .injectEndpoints({
//     endpoints: (builder) => ({
//       requestProfile: builder.query<UserProfileResponse, undefined>({
//         query: () => ({ url: "/user/profile"}),
//         providesTags: ["profile"], // 
//         async onQueryStarted(arg, api) {
//             const response = await api.queryFulfilled;
//             api.dispatch(saveUser(response.data.data));
//         },
//       }),
//     }),
// });
// {profile:requestProfile的响应数据}

// export const { useLazyRequestProfileQuery } = userApiService
export const userApiService = apiService
  .enhanceEndpoints({ addTagTypes: ["profile"] })
  .injectEndpoints({
    endpoints: (builder: any) => ({
      // 获取用户资料
      requestProfile: builder.query({
        async onQueryStarted(arg: any, api: any) {
          const response = await api.queryFulfilled;
          api.dispatch(saveUser(response.data.data));
        }
      })
    })
  })
apiService.injectEndpoints({
  endpoints: (builder:any) => ({
    // 获取登录用户信息
    requestUser: builder.query({
      query: () => ({
        url: "/user",
        headers: {
          authorization: `Bearer ${store.getState().credentialsReducer.token}`,
        },
      }),
    }),
  }),
});
// export const { useRequestUserQuery } = userApiService;